<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=640px,user-scalable=no,target-densitydpi=device-dpi" />
		<link rel="stylesheet" href="../css/css.css" type="text/css" />
		<script type="text/javascript" src='../js/jquery.min.js'></script>
		<script type="text/javascript" src='../js/main_in.js'></script>
		<script type="text/javascript" src='../js/common.js'></script>
		<script type="text/javascript" src='../js/mui.min.js'></script>
		<title>主页</title>
		<script>
var scard1 = '';
var scard2 = '';
var dcard1 = '';
var dcard2 = '';
var bcard = '';
			var uid = window.localStorage["uid"];
			$(function(){
				$("#ismail").change(function() {
					var ismail = $(this).val();
					if(ismail == 0){
						$("#isaddr").css("display","none");
					}else{
						$("#isaddr").css("display","block");
					}
				});
			});
			
			
// 上传文件
var server="http://insure.unair.cn/upload_h.php";
var files=[];

function upload(divid){
	if(files.length<=0){
		plus.nativeUI.alert("没有添加上传文件！");
		return;
	}
	var wt=plus.nativeUI.showWaiting();
	var task=plus.uploader.createUpload(server,
		{method:"POST"},
		function(t,status){ //上传完成
			if(status==200){
				plus.storage.setItem("uploader",t.responseText);
				data = eval("(" + t.responseText + ")");
//				alert(JSON.stringify(data))
					$("#" + divid).children().remove();
					
				if(divid == 'idcardimg1'){
					url = "http://insure.unair.cn/" + data['files']['uploadkey']['url'];
					var src = '<img src="' +url+ '" style="width: 200px;height:150px;"/>';
					scard1 = url;
					$("#idcardimg1").append(src);
				}
				if(divid == 'idcardimg2'){
					url = "http://insure.unair.cn/" + data['files']['uploadkey']['url'];
					var src = '<img src="' +url+ '" style="width: 200px;height:150px;"/>';
					scard2 = url;
					$("#idcardimg2").append(src);
				}
				if(divid == 'driverimg1'){
					url = "http://insure.unair.cn/" + data['files']['uploadkey']['url'];
					var src = '<img src="' +url+ '" style="width: 200px;height:150px;"/>';
					dcard1 = url;
					$("#driverimg1").append(src);
				}
				if(divid == 'driverimg2'){
					url = "http://insure.unair.cn/" + data['files']['uploadkey']['url'];
					var src = '<img src="' +url+ '" style="width: 200px;height:150px;"/>';
					dcard2 = url;
					$("#driverimg2").append(src);
				}
				if(divid == 'bankimg'){
					url = "http://insure.unair.cn/" + data['files']['uploadkey']['url'];
					var src = '<img src="' +url+ '" style="width: 200px;height:150px;"/>';
					bcard = url;
					$("#bankimg").append(src);
				}
					
					
				wt.close();
		}else{
				wt.close();
			}
		}
	);
	task.addData("client","HelloH5+");
	task.addData("uid",getUid());
	for(var i=0;i<files.length;i++){
		var f=files[i];
		task.addFile(f.path,{key:f.name});
	}
	task.start();
}
		// 拍照添加文件
function appendByCamera(divid){
	plus.camera.getCamera().captureImage(function(p){
		appendFile(p);
		upload(divid);
	});	
}
// 从相册添加文件
function appendByGallery(divid){
	plus.gallery.pick(function(p){
        appendFile(p);
        upload(divid);
    });
}
// 添加文件
var index=1;
function appendFile(p){
	var fe=document.getElementById("files");
	var li=document.createElement("li");
	var n=p.substr(p.lastIndexOf('/')+1);
	li.innerText = n
	fe.appendChild(li);
	files.push({name:"uploadkey",path:p});
	index++;
	empty.style.display="none";
}
// 产生一个随机数
function getUid(){
	return Math.floor(Math.random()*100000000+10000000).toString();
}


function check(){
	
	var engine = $("#engine").val();
	var idcard = $("#idcard").val();
	
	if(engine == ''){
		alert('发动机编号不能为空');
	}
	if(idcard == ''){
		alert('身份证号码不能为空');
	}
	
	var wt=plus.nativeUI.showWaiting();
	$.ajax({
		url:'http://insure.unair.cn/etc_check.php',
		type:'POST',
		data:{
			engine:engine,
			idcard:idcard
		},
		success: function(data) {
			wt.close();
			if(data == 1){
				doadd();
			}
			if(data == 2){
				alert("该行驶证已申请过ETC!");
				return false;
			}
			if(data == 3){
				alert("该身份证已绑定两张行驶证，无法再次申请");
				return false;
			}
			
			
        },
        error:function(data){
        	wt.close();
        }
	})
	
}


function doadd(){
	
	var bankcard = $("#bankcard").val();
	var bankaddr = $("#bankaddr").val();
	var address = $("#address").val();
	var engine = $("#engine").val();
	var idcard = $("#idcard").val();
	var urgencontact = $("#urgencontact").val();
	var urgentel = $("#urgentel").val();
	var carcolor = $("#carcolor").val();
	var ismail = $("#ismail").val();
	
	if(bankcard == ''){
		alert('银行卡号不能为空');
	}
	if(bankaddr == ''){
		alert('开户行不能为空');
	}
	if(urgencontact == ''){
		alert('紧急联系人不能为空');
	}
	if(urgentel == ''){
		alert('紧急联系人号码不能为空');
	}
	if(carcolor == ''){
		alert('车头颜色不能为空');
	}
	
	if(ismail == 0){
		var wt=plus.nativeUI.showWaiting();
		$.ajax({
			url:'http://insure.unair.cn/etcadd.php',
			type:'POST',
			data:{
				scard1:scard1,
				scard2:scard2,
				dcard1:dcard1,
				dcard2:dcard2,
				bcard:bcard,
				bankcard : bankcard,
				bankaddr:bankaddr,
				address : address,
				engine : engine,
				idcard:idcard,
				urgencontact:urgencontact,
				urgentel:urgentel,
				carcolor:carcolor,
				ismail:0,
				ispay : 0,
				apid:'',
				uid:uid
			},
			success: function(data) {
				wt.close();
				if(data == 1){
					alert("申请成功");
					clicked('./search.html');
				}
	        },
		})
	}else{
		if(address == ''){
			alert('收货地址不能为空');
		}
		var dd = new Date();
		var apid = '';
			apid += dd.getFullYear();
			apid += dd.getMonth() + 1;
			apid += dd.getDate();
			apid += dd.getHours();
			apid += dd.getMinutes();
			apid += dd.getSeconds();
			apid += parseInt(899 * Math.random() + 100);
		
		var wt=plus.nativeUI.showWaiting();
		$.ajax({
			url:'http://insure.unair.cn/etcadd.php',
			type:'POST',
			data:{
				scard1:scard1,
				scard2:scard2,
				dcard1:dcard1,
				dcard2:dcard2,
				bcard:bcard,
				bankcard : bankcard,
				address : address,
				engine : engine,
				ispay : 1,
				apid:apid,
				uid:uid,
				
				bankaddr:bankaddr,
				idcard:idcard,
				urgencontact:urgencontact,
				urgentel:urgentel,
				carcolor:carcolor,
				ismail:1,
			},
			success: function(data) {
				if(data == 1){
					clicked("./payment.html?apid=" + apid)
				}
				wt.close();
	            
									
	        },
		})
	}
	
	
}
				
		</script>
	</head>
	<body>
		
		<div class="app_head">
            <div class="app_prev" onclick="clicked('./search.html')"><img src="../img/arrow.png" /></div>
            <div class="app_head_text">ETC申请</div>
            <div class="app_prev"></div>
        </div>
        <ul id="files" style="text-align:left;display:none;">
				<p id="empty" style="font-size:12px;color:#C6C6C6;">无上传文件</p>
		</ul>
        <div class="app_body">
        	<div class="main_in_input_image" >
                <div class="main_in_input_image_label">请上传正反面身份证照片：</div>
                <div onclick="showActionSheet(this)" id="idcardimg1" class="main_in_input_image_pit" style="float: left;">
                    <span id="jiajia" style="font-size:105px;">+</span>
                </div>
                <div onclick="showActionSheet(this)" id="idcardimg2" class="main_in_input_image_pit" style="float: left;margin-left: 40px;">
                    <span id="jiajia" style="font-size:105px;">+</span>
                </div>
            </div>
            <div class="main_in_input">
				<div class="main_in_input_text_left">身份证号：</div>
				<div class="main_in_input_input_right"><input type="text" id="idcard" value="" /></div>
			</div>
            <div class="main_in_input_image" >
                <div class="main_in_input_image_label">请上传正反面行驶证照片：</div>
                <div onclick="showActionSheet(this)" id="driverimg1" class="main_in_input_image_pit" style="float: left;">
                    <span id="jiajia" style="font-size:105px;">+</span>
                </div>
                <div onclick="showActionSheet(this)" id="driverimg2" class="main_in_input_image_pit" style="float: left;margin-left: 40px;">
                    <span id="jiajia" style="font-size:105px;">+</span>
                </div>
            </div>
            <div class="main_in_input">
				<div class="main_in_input_text_left">发动机编号：</div>
				<div class="main_in_input_input_right"><input type="text" id="engine" value="" /></div>
			</div>
            <div class="main_in_input_image" >
                <div class="main_in_input_image_label">请上传非邮政、农业银行和本地工商银行卡照片：</div>
                <div onclick="showActionSheet(this)" id="bankimg" class="main_in_input_image_pit">
                    <span id="jiajia" style="font-size:105px;">+</span>
                </div>
            </div>
            <div style="clear: both;"></div>
            <div class="main_in_input">
				<div class="main_in_input_text_left">银行卡号：</div>
				<div class="main_in_input_input_right"><input type="text" id="bankcard" value="" /></div>
			</div>
			<div class="main_in_input">
				<div class="main_in_input_text_left">银行卡开户行：</div>
				<div class="main_in_input_input_right"><input type="text" id="bankaddr" value="" placeholder="具体到市即可"/></div>
			</div>
			<div class="main_in_input">
				<div class="main_in_input_text_left">紧急联系人：</div>
				<div class="main_in_input_input_right"><input type="text" id="urgencontact" value="" /></div>
			</div>
			<div class="main_in_input">
				<div class="main_in_input_text_left">联系人电话：</div>
				<div class="main_in_input_input_right"><input type="text" id="urgentel" value="" /></div>
			</div>
			<div class="main_in_input">
				<div class="main_in_input_text_left">车头颜色：</div>
				<div class="main_in_input_input_right"><input type="text" id="carcolor" value="" /></div>
			</div>
			<div class="main_in_input">
				<div class="main_in_input_text_left">是否邮寄：</div>
				<div class="main_in_input_input_right">
					<select class='app_input_text' id="ismail">
						<option value='1'>是</option>
						<option value='0'>否</option>
					</select>
				</div>
			</div>
        	<div class="main_in_input" id="isaddr">
				<div class="main_in_input_text_left">收货地址：</div>
				<div class="main_in_input_input_right"><input type="text" id="address" value="" /></div>
			</div>
        	<div style="background: #fff;width: 100%;height: 100px;">
				<div class="bg_submit" onclick="check()">提&nbsp;交</div>
				<div class="bg_back_class" onclick="clicked('./search.html')">返&nbsp;回</div>
			</div>
        </div>
        	

	</body>
	<script type="text/javascript" src="../js/immersed.js" ></script>
	<script>
		function showActionSheet(conf){
          var divid = conf.id; 
          
              var actionbuttons=[{title:"拍照"},{title:"相册"}];  
              var actionstyle={title:"选择照片",cancel:"取消",buttons:actionbuttons};  
              plus.nativeUI.actionSheet(actionstyle, function(e){  
                    if(e.index==1){
                        appendByCamera(divid)
                    }else if(e.index==2){  
                        appendByGallery(divid)
                    }  
              } );  
        }

	</script>
</html>
